package com.wash.framework.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import java.util.List;

public abstract class JsonUtil
{
  public static <T> T readObject(String json, Class<T> clazz)
  {
    if ((null == json) || ("".equals(json))) {
      return null;
    }
    Object obj = null;
    ObjectMapper om = new ObjectMapper();
    ObjectReader reader = om.reader(clazz);
    reader.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    try {
      obj = reader.readValue(json);
      return clazz.cast(obj); } catch (Exception e) {
    }
    return null;
  }

  public static String writeObject(Object obj)
  {
    if (null == obj) {
      return null;
    }
    String json = null;
    ObjectMapper om = new ObjectMapper();
    ObjectWriter writer = om.writer();
    try {
      json = writer.writeValueAsString(obj);
    } catch (JsonProcessingException e) {
      return null;
    }
    return json;
  }

  public static <T> List<T> convertListObject(String jsons, TypeReference typeRefe) throws Exception {
    ObjectMapper jacksonMapper = new ObjectMapper();
    return (List)jacksonMapper.readValue(jsons, typeRefe);
  }
}